<template>
  <view class="submit_order">
    <!-- 适配状态栏 -->
    <view class="status_bar"></view>
    <!-- #ifndef MP-WEIXIN -->
    <view class="top_title flex-btw">
      <view>
        <text
          class="custom-icon custom-icon-jiantou2"
          @click="leftClick"
        ></text>
        <text class="name">确认订单</text>
      </view>
      <view>
        <view
          class="flex-middle"
          @click="showChild = true"
          v-if="childList.length > 0"
        >
          <image
            src="../../static/image/order_user.png"
            class="icon_user"
            mode=""
          ></image>
          <text class="account">{{ child.name }}</text>
          <image
            class="icon_right"
            src="../../static/image/order_right.png"
            mode=""
          ></image>
        </view>
        <u-select
          v-model="showChild"
          mode="single-column"
          :list="childList"
          value-name="id"
          label-name="nickname"
          @confirm="selectChild"
        ></u-select>
      </view>
    </view>
    <!--  #endif -->
    <!-- #ifdef MP-WEIXIN -->
    <view class="top_title flex-btw" v-if="false">
      <view></view>
      <view>
        <view
          class="flex-middle"
          @click="showChild = true"
          v-if="childList.length > 0"
        >
          <image
            src="../../static/image/order_user.png"
            class="icon_user"
            mode=""
          ></image>
          <text class="account">{{ child.name }}</text>
          <image
            class="icon_right"
            src="../../static/image/order_right.png"
            mode=""
          ></image>
        </view>
        <u-select
          v-model="showChild"
          mode="single-column"
          :list="childList"
          value-name="id"
          label-name="nickname"
          @confirm="selectChild"
        ></u-select>
      </view>
    </view>
    <!-- #endif -->
    <!-- <view v-if="childList.length>=1" class="flex flex_row_between child" @click="showChild=true">
			<text>切换账户</text>
			<view class="flex-middle">
				<u-select v-model="showChild" mode="single-column" :list="childList" value-name="id" label-name="nickname" @confirm="selectChild"></u-select>
				
				<text style="color:#191919">{{child.name}}</text>
				<u-icon name="arrow-right" color="gray" size="20"></u-icon>
			</view>
		</view> -->
    <view class="flex flex_row_between padding address_info">
      <view class="flex bgColor" style="width: 100%">
        <image
          class="location"
          src="https://copartner.oss-cn-beijing.aliyuncs.com/logo/2401/12/4oeTIaD2Qpkl0IT3WQEtNcTq0VJ6ihKPUAXFIM3e.png"
        ></image>
        <view class="address">
          <view>
            <view class="add bold fonts_34"> {{ nowSelectInfo.address }} </view>
            <view class="name sixteen add">
              {{ nowSelectInfo.name }} {{ nowSelectInfo.mobile }}
            </view>
          </view>
        </view>
        <!-- <view style="margin-left: 10rpx">
          <u-icon name="arrow-right" color="gray" size="20"></u-icon>
        </view> -->
      </view>
    </view>
    <div
      class="bgColor"
      style="padding: 20rpx 0rpx; width: 690rpx; margin: 0 auto"
    >
      <view class="goods">
        <view class="tit">{{
            `${dataDetail.name} (${dataDetail.date_range})`
          }}</view>
        <my-recipelist :list="dataDetail.packageMenuList"></my-recipelist>
      </view>
      <view class="goods" v-if="pointList.length > 0">
        <view class="tit">积分</view>
        <view class="goodsList">
          <view
            class="list flex-btw"
            v-for="item in pointList.slice(0, jfListNum)"
            :key="item.id"
          >
            <view class="flex-middle">
              <view class="imgs flex-middle">
                <img :src="item.img" class="img" mode="aspectFit" />
              </view>
              <view class="info">
                <view class="flex-middle">
                  <text class="red">{{ item.jf_points }}积分</text>
                </view>
                <view
                  >{{ item.goods_name }}
                  <text class="spu">{{ item.spu_name }}</text></view
                >
              </view>
            </view>
            <text>× {{ item.num }}</text>
          </view>
        </view>
        <view
          @click="expendList('isexpend_jf', 'jfListNum', pointList.length)"
          class="flex-center"
          style="margin-top: 14rpx"
        >
          <u-icon name="arrow-up" v-if="isexpend_jf"></u-icon>
          <u-icon name="arrow-down" v-else></u-icon>
          共{{ pointList.length }}件
        </view>
      </view>
      <view class="goods" v-if="zpList.length > 0">
        <view class="tit">赠品</view>
        <view class="goodsList">
          <view
            class="list flex-btw"
            v-for="item in zpList.slice(0, zpListNum)"
            :key="item.id"
          >
            <view class="flex-middle">
              <view class="imgs flex-middle">
                <img :src="item.img" class="img" mode="aspectFit" />
              </view>
              <view class="info">
                <view class="flex-middle">
                  <!-- <text class="red">{{item.jf_zps}}积分</text> -->
                </view>
                <view
                  >{{ item.goods_name }}【{{ item.spu_name }}】
                  <text class="yellow_tag">赠品</text></view
                >
              </view>
            </view>
            <text>× {{ item.num }}</text>
          </view>
        </view>
        <view
          @click="expendList('isexpend_zp', 'zpListNum', zpList.length)"
          class="flex-center"
          style="margin-top: 14rpx"
        >
          <u-icon name="arrow-up" v-if="isexpend_zp"></u-icon>
          <u-icon name="arrow-down" v-else></u-icon>
          共{{ zpList.length }}件
        </view>
      </view>
      <view class="select padding sixteen">
        <view class="flex-btw fonts_26" style="margin-bottom: 26rpx">
            <text>配送日期</text>
            <text>{{ dataDetail.date_range }}</text>
          </view>
        <!-- <picker mode="date">
          <view class="flex-btw fonts_26" style="margin-bottom: 26rpx">
            <text>配送日期</text>
            <text>{{ dataDetail.date_range }}</text>
          </view>
        </picker> -->
        <view @click="showTime = true" class="fonts_26">
          <text>配送时间</text>
          <view class="flex-middle">
            <text style="color: #191919">{{ sendTimeName }}</text>
            <u-select
              v-model="showTime"
              mode="single-column"
              :list="send_time"
              value-name="id"
              label-name="time"
              @confirm="selectTime"
            ></u-select>
            <u-icon name="arrow-right" color="gray" size="20"></u-icon>
          </view>
        </view>
        <view class="fonts_26">
          <text>商品总金额</text>
          <view>
            <text style="color: #191919"
              >￥{{ dataDetail.order_amount }}</text
            >
          </view>
        </view>


        <view class="fonts_26">
          <text>运费</text>
          <text style="color: #191919">￥{{ 0 }}</text>
        </view>

        <view class="fonts_26">
          <text>差额</text>
          <view>
            <text style="color: #191919">￥{{ 0 }}</text>
          </view>
        </view>

        <view class="fonts_26">
          <text>起送价</text>
          <view>
            <text style="color: #191919">￥{{ 0 }}</text>
          </view>
        </view>

        <view class="fonts_26">
          <text>下单金额</text>
          <view>
            <text style="color: #191919"
              >￥{{ dataDetail.order_amount }}</text
            >
          </view>
        </view>
        <view @click="showPay = true" class="fonts_26">
          <text>支付方式</text>
          <view class="flex-middle">
            <picker
              mode="selector"
              :range="payList"
              :value="payWay"
              range-key="label"
              @change="selectPay"
            >
              <text style="color: #191919">{{ payList[payWay].label }}</text>
              <u-icon name="arrow-right" color="gray" size="20"></u-icon>
            </picker>
          </view>
        </view>
      </view>
      <view
        class="infomation"
        v-if="
          this.pay.name == '货到付款' && overdue.overdue_status != 0
            ? true
            : false
        "
      >
        <view :class="[{ error: overdue.overdue_status == 2 }, ' info']">{{
          overdue.info
        }}</view>
        <view :class="[{ error: credit.credit_status == 2 }, ' info']">{{
          credit.info
        }}</view>
      </view>
      <view
        class="infomation"
        v-if="
          this.pay.name == '货到付款' && credit.credit_status != 0
            ? true
            : false
        "
      >
        <view :class="[{ error: overdue.overdue_status == 2 }, ' info']">{{
          overdue.info
        }}</view>
        <view :class="[{ error: credit.credit_status == 2 }, ' info']">{{
          credit.info
        }}</view>
      </view>
    </div>
    <view
      class="remark fonts_26 bgColor"
      style="
        width: 690rpx;
        margin: 20rpx auto;
        padding: 20rpx 0 0 34rpx;
        box-sizing: border-box;
      "
    >
      <text class="remark_title">菜谱备注</text>
      <textarea
        :value="note"
        placeholder=""
        auto-height
        :disabled="true"
      ></textarea>
    </view>
    <view class="submit_height"></view>
    <view class="submit flex flex_col_center">
      <view class="sixteen flex-center">
        <text class="bold fonts_26">总计：</text>
        <view class="flex-column" style="align-items: flex-end">
          <text class="fonts_24 red_font">￥{{ dataDetail.order_amount }}</text>
        </view>
      </view>
      <view class="order twelve_font fonts_26" @click="submit"> 提交订单 </view>
    </view>
    <u-mask :show="showMask"></u-mask>
  </view>
</template>

<script>
let app = getApp();
import { $https } from "@/static/js/request.js";
import MixinShowprice from "@/static/js/mixinshowprice.js";
export default {
  data() {
    return {
      showMask: false,
      showChild: false, //选择子账户弹窗
      childList: [],
      showPay: false, //选择支付方式弹窗
      payList: [
        {
          label: "在线支付",
          value: 1,
        },
        {
          label: "货到付款",
          value: 0,
        },
      ],
      payWay: 0,
      pay: {
        name: "",
        id: null,
        index: 0,
      },
      showTime: false,
      //主账号信息：用户地址、收货人、号码
      info: {
        address: null,
        name: null,
        mobile: null,
        id: null,
      },
      nowSelectInfo: {}, //当前选择的账号地址信息
      //当前选择的账号（用于选择器显示）
      child: {
        id: null,
        name: "",
      },
      sendDate: null, //选择的配送日期
      // sendDateM:null,	//配送日期转毫秒
      send_time: [
        {
          time: "不限",
          id: 0,
        }
      ], //配送时间列表
      sendTimeId: null, //选择的配送时间id
      sendTimeName: "不限", //选择的配送时间
      note: "", //备注
      goodsId: null, //商品id
      money: {}, //金钱相关（差额，小计，合计，积分，运费）
      couponInfo: {
        coupon_now: {
          coupons: {},
        },
      }, //优惠券相关

      isexpend: false,
      goodsList: [],
      goodsListNum: 1,
      isexpend_jf: false,
      pointList: [], //积分商品
      jfListNum: 1,
      isexpend_zp: false,
      zpList: [], //赠品
      zpListNum: 1,

      overdue: {}, //账期
      credit: {}, //信用金
      backDay: 0, //开始日期回退天数
      startDay: this.tomorrow(),
      quadrants_setting: {},
      dataDetail: {},
      showAllData: false,
      goodsData: {},
      menuImg: '../../static/image/menu-img.png'
    };
  },
  mixins: [MixinShowprice],
  onReady() {
    getApp().setTitle();
  },
  computed: {
    dataList() {
      if(this.dataDetail.packageMenuList && this.dataDetail.packageMenuList.length) {
        if(this.dataDetail.packageMenuList.length > 2) {
          return this.showAllData ? this.dataDetail.packageMenuList : this.dataDetail.packageMenuList.slice(0, 2);
        }else {
          return this.dataDetail.packageMenuList
        }
      }else {
        return []
      }
    }
  },
  onLoad(options) {
    this.goodsData = JSON.parse(options.goodsData)
    this.note = options.remark;
    this.getData(this.goodsData);
    this.sendDate = this.tomorrow();
  },
  onShow() {
    // this.getData();
    // uni.$once("selectCoupon", (data) => {
    //   this.findCoupon(data.id);
    // });
  },
  methods: {
    changeAllData() {
      this.showAllData = !this.showAllData
    },
    getData(params) {
      uni.showLoading({
        title: "加载中",
      });
      $https("GET", "menuConfirmOrderDetail", { ...params }, (res) => {
        if (res.data.errcode == 0) {
          uni.hideLoading();
          const data = res.data.data
          data.packageMenuList = data.packageMenuList.map(el => {
            return {
              lookDetail: false,
              ...el
            }
          })
          console.log(data);
          
          this.dataDetail = data
        } else {
          uni.hideLoading();
          uni.showToast({
            title: res.data.errmsg,
            icon: "none",
          });
        }
      });
      $https("GET", "userAddress", { }, (res) => {
        if (res.data.errcode == 0) {
          this.nowSelectInfo = res.data.data
        } else {
          uni.showToast({
            title: res.data.errmsg,
            icon: "none",
          });
        }     
      })
    },
    changeLookDetail(item) {  //  查看详情
      this.$set(item, 'lookDetail', !item.lookDetail)
    },
    gopage() {
      uni.navigateTo({
        url: "/subindex/shopcart/check_wx",
      });
    },
    //筛选商品、积分商品
    filterGoods(datas) {
      let goodsList = [],
        pointList = [];
      datas.map((item) => {
        if (item.datum_activity === "积分") {
          pointList.push(item);
        } else {
          goodsList.push(item);
        }
      });
      this.pointList = pointList;
      this.goodsList = goodsList;
    },
    //根据列表、id查到对应优惠券
    findCoupon(id, isfirst) {
      let list = this.couponInfo.coupons_list;
      for (let a in list) {
        if (list[a].id === id) {
          Object.assign(this.couponInfo, {
            coupon_now: list[a],
            coup_id: id,
          });
          break;
        }
      }
      if (!isfirst) {
        this.getNewPrice();
      }
    },
    //去选择优惠券
    toChooseCoupon() {
      setTimeout((res) => {
        uni.$emit("getSubOrdCou", {
          couponInfo: this.couponInfo,
        });
      }, 500);
      uni.navigateTo({
        url: "/subpack/user/coupon/coupon_choose",
      });
    },
    //选择子账号
    selectChild(val) {
      this.child.name = val[0].label;
      this.child.id = val[0].value;
      let data = val[0].extra;
      this.nowSelectInfo = {
        address: data.address_name + data.detail,
        mobile: data.mobile,
        name: data.name,
        id: val[0].value == this.info.user_id ? null : val[0].value, //如果选择的是主账号，不给id
      };
      this.choseData(this.child.id);
    },

    choseData(val) {
      $https("GET", "isWithin", { uid: val }, (res) => {
        if (res.data.errcode == 0) {
          this.quadrants_setting = res.data.data;
        } else {
          uni.showToast({
            title: res.data.errmsg,
            icon: "none",
          });
        }
      });
    },

    //选择支付方式
    selectPay(e) {
      this.payWay = e.detail.value;
    },
    //选择日期
    selectDate(val) {
      this.sendDate = val.detail.value;
    },
    //选择时间
    selectTime(val) {
      // this.sendTimeId = val[0].value;
      // this.sendTimeName = val[0].label;
    },
    tomorrow(backday) {
      var now = new Date();
      //是否是设定开始日期。按请求数据回退
      if (backday) {
        now.setTime(now.getTime() - (backday - 1) * 24 * 60 * 60 * 1000);
      } else {
        now.setTime(now.getTime() + 24 * 60 * 60 * 1000);
      }
      var tom = `${now.getFullYear()}-${
        now.getMonth() + 1 < 10
          ? "0" + (now.getMonth() + 1)
          : now.getMonth() + 1
      }-${now.getDate() < 10 ? "0" + now.getDate() : now.getDate()}`;
      return tom;
    },
    leftClick() {
      // #ifdef H5
      window.history.back(-1);
      // #endif
      // #ifndef H5
      uni.navigateBack();
      // #endif
    },
    //备注
    noteAction(e) {
      this.note = e.detail.value;
    },
    //展开
    expendList(flag, num, length) {
      this[flag] = !this[flag];
      if (this[flag]) {
        this[num] = length;
      } else {
        this[num] = 1;
      }
      // this.isexpend = !this.isexpend;
      // if(this.isexpend){
      // 	this.goodsListNum = this.goodsList.length;
      // }else{
      // 	this.goodsListNum = 1;
      // }
    },
    submit() {
      let params = {
        id: this.goodsData.id,
        order_detail: this.goodsData.order_num_arr,
        remark: this.note
      }
      $https("POST", "addMenuOrder", params, (res) => {
        if (res.data.errcode == 0) {
          uni.switchTab({
            url:'/pages/tabar/order'
          })
        } else {
          uni.showToast({
            title: res.data.errmsg,
            icon: "none",
          });
        }
      });
    },
    //修改地址
    editAdd() {
      if (this.nowSelectInfo.id) {
        uni.navigateTo({
          url: `/subpack/user/change/edit_address?zid=${this.nowSelectInfo.id}`,
        });
      } else {
        uni.navigateTo({
          url: `/subpack/user/change/edit_address`,
        });
      }
      // if(!this.nowSelectInfo.id){
      // uni.navigateTo({
      // 	url:`/subpack/user/change/edit_address?address=${this.info.address}&name=${this.info.name}&tel=${this.info.mobile}`
      // })
      // }
    },
  },
};
</script>

<style lang="scss" scoped>
.list {
  .list-detail {
    display: flex;
    width: 100%;
    .info {
      flex: 1;
    }
    .info-time {
      font-weight: bold;
      font-size: 30rpx;
      color: #000000;
    }
    .info-num {
      font-size: 24rpx;
      color: #999999;
    }
    .info-look {
      font-size: 24rpx;
      color: #333;
      margin-top: 29rpx;
      text {
        margin-right: 10rpx;
      }
    }
    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
  .info-pirce {
    color: #f55726;
    font-size: 36rpx;
  }
  .info-good-wrap {
    padding: 30rpx;
    background: #F8F9FA;
    border-radius: 20rpx;
    margin: 20rpx 0;
    .info-goods {
      .mg-t-20 {
        margin-top: 20rpx;
      }
      .info-goods-name {
        font-size: 24rpx;
        color: rgba(0, 0, 0, .95);
        font-weight: bold;
        margin-bottom: 20rpx;
      }
      .info-goods-list {
        margin: 20rpx 0;
        &:last-child {
          margin-bottom: 0;
        }
        display: flex;
        text:first-child {
          width: 45%;
        }
        text:nth-child(2) {
          width: 25%;
          padding-right: 10%;
          text-align: right;
        }
        text:last-child {
          flex: 1;
          text-align: right;
        }
      }
    }
  }
}
.submit_order {
  .status_bar {
    position: fixed;
    top: 0;
  }

  .bgColor {
    background: white;
    border-radius: 20rpx;
    padding: 20rpx;
    box-sizing: border-box;
  }
  .top_title {
    position: fixed;
    width: 100%;
    // #ifndef MP-WEIXIN
    top: calc(var(--status-bar-height));
    // #endif
    // #ifdef MP-WEIXIN
    top: 0;
    // #endif
    background: rgb(246, 246, 246);
    align-items: center;
    height: 88rpx;
    padding: 0 30rpx;
    z-index: 100;

    .custom-icon {
      font-size: 36rpx;
      color: #191919;
      margin-right: 36rpx;
      font-weight: 600;
    }

    .name {
      color: #191919;
      font-size: 36rpx;
      font-weight: 600;
    }

    .icon_user {
      width: 26rpx;
      height: 30rpx;
    }

    .icon_right {
      width: 12rpx;
      height: 16rpx;
      margin-left: 10rpx;
    }

    .account {
      margin-left: 10rpx;
    }
  }

  /deep/.u-calendar {
    background: #fff;
    padding-top: 10rpx;
  }

  .child {
    padding: 28rpx 34rpx;
  }

  .caution {
    background: #f6e8e5;
    color: #666;

    .tip {
      color: #fe5324;
    }
  }

  .goods {
    color: #333;
    font-size: 28rpx;

    .tit {
      font-size: 30rpx;
      color: #000;
      font-weight: bold;
      margin: 0 30rpx 20rpx;
      border-bottom: 1rpx solid #ECECEC;
      padding-bottom: 20rpx;
    }

    .goodsList {
      margin: 0 40rpx 0 30rpx;
      font-size: 28rpx;
      border-bottom: 2rpx solid #e7e8e8;

      .list {
        margin-bottom: 20rpx;

        .imgs {
          width: 100rpx;
          height: 100rpx;
          margin-right: 30rpx;
          position: relative;

          .logo {
            width: 40rpx;
            height: 40rpx;
            position: absolute;
            top: 0;
            left: 0;
            z-index: 1;
          }

          .img {
            width: 100rpx;
            max-height: 100rpx;
          }
        }

        .info {
          .red {
            color: red;
            margin-right: 10rpx;
            font-size: 26rpx;
          }

          .red_tag {
            display: inline-block;
          }

          .yellow_tag {
            font-size: 20rpx;
            padding: 2rpx 12rpx;
            background: #ff9900;
            border-radius: 6rpx;
            color: #fff;
          }

          .spu {
            margin-left: 20rpx;
            padding: 0 8rpx;
            background: #e3e3e3;
            border-radius: 8rpx;
            font-size: 24rpx;
          }
        }
      }
    }
  }

  .select > view {
    display: flex;
    justify-content: space-between;
  }

  .select > view view:first-child text {
    font-weight: bold;
  }

  .select > view view:last-child text {
    color: gray;
  }

  .select .remark {
    flex-direction: column;
  }

  .submit {
    height: 50px;
    position: fixed;
    bottom: 0;
    background: $white;
    justify-content: flex-end;
    box-shadow: 1px 1px 2px rgba(0, 0, 0, 1);

    .order {
      background: $dgreen;
      color: $white;
    }
  }
}

.submit_order {
  .address_info {
    // #ifndef MP-WEIXIN
    margin-top: calc(var(--status-bar-height) + 88rpx);
    // #endif
    // #ifdef MP-WEIXIN
    margin-top: 20rpx;
    // #endif
    padding: 0 30rpx 28rpx;
    // border-bottom: 16rpx solid #e7e8e8;

    .caution {
      padding: 4rpx 24rpx;

      .tip {
        margin-right: 12rpx;
      }
    }

    .address > view {
      margin-bottom: 4rpx;
    }
    .address {
      width: 88%;
      display: flex;
      justify-content: space-between;
    }
  }

  .select {
    padding-top: 54rpx;
    padding-bottom: 0;

    .remark textarea {
      margin-top: 20rpx;
    }
  }

  .select > view {
    margin-bottom: 26rpx;
  }
  .submit_height {
    height: calc(154rpx + env(safe-area-inset-bottom));
  }
  .submit {
    height: 134rpx;
    width: calc(100% - 88rpx);
    padding: 0 44rpx;
    z-index: 10;
    padding-bottom: env(safe-area-inset-bottom);
    box-sizing: content-box;

    .order {
      padding: 24rpx 42rpx;
      border-radius: 50rpx;
      margin-left: 40rpx;
      font-size: 32rpx;
    }
    .bold {
      font-size: 32rpx;
    }
    .red_font {
      font-size: 30rpx;
    }
  }
}

.submit_order {
  color: #191919;

  .address_info {
    .location {
      width: 30rpx;
      height: 36rpx;
      margin-right: 24rpx;
      margin-top: 5rpx;
    }

    .address {
      color: #191919;
      font-size: 32rpx;

      .name {
        margin: 12rpx 0;
      }

      .caution {
        padding: 8rpx 20rpx;
        font-size: 24rpx;
        margin: 0;
      }
    }
  }

  .remark {
    &_title {
      font-size: 32rpx;
    }
    textarea {
      width: 100%;
      font-size: 24rpx;
      min-height: 200rpx;
      margin-top: 10rpx;
    }
  }

  .infomation {
    position: fixed;
    bottom: 134rpx;
    left: 0;
    right: 0;
    color: #fff;
    font-size: 10px;

    .info {
      padding-left: 20rpx;
      background: #fea85f;

      &.error {
        background: #e74c4a;
      }
    }
  }
}

.name_flex {
  display: flex;
  align-items: center;
}

.add {
  font-size: 26rpx;
  color: #333;
}
</style>
